package com.project.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;



@Component
public class SysConfig implements WebMvcConfigurer {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //放行html路径下的所有
        registry.addResourceHandler("/html/**")
                .addResourceLocations("classpath:/html/");//引导到文件路径
        registry.addResourceHandler("/img/**")
                .addResourceLocations("file:d:/img/");
    }

    //此服务器允许的跨域访问
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")  //跨域的URL
                .allowedHeaders("*")  //请求头
                .allowedMethods("*")  //请求方法
                .allowedOrigins("*");  //请求域名
    }

    //把restTemplate 交给spring
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
